package com.sokal.easycare.modules.product.service.product;

import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.service.CrudService;
import com.sokal.easycare.common.utils.CacheUtils;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.common.utils.FastDFS.FastDFSClient;
import com.sokal.easycare.modules.product.dao.product.ProAttributeDao;
import com.sokal.easycare.modules.product.dao.product.ProProductFileDao;
import com.sokal.easycare.modules.product.entity.product.ProAttribute;
import com.sokal.easycare.modules.product.entity.product.ProProduct;
import com.sokal.easycare.modules.product.entity.product.ProProductFile;
import com.sokal.easycare.modules.sys.entity.Menu;
import com.sokal.easycare.modules.sys.utils.LogUtils;
import com.sokal.easycare.modules.sys.utils.UserUtils;

/**
 * 产品Service
 * @author liliangdong
 * @version 2017-03-12
 */
@Service
@Transactional(readOnly = true)
public class ProProductFileService extends CrudService<ProProductFileDao, ProProductFile> {

	@Autowired
	private ProProductFileDao proProductFileDao;
	
	
	public List<ProProductFile> findList(ProProductFile proProductFile) {
		return super.findList(proProductFile);
	}
	
	@Transactional(readOnly = false)
	public void save ( MultipartFile productFile, String productId) throws Exception{
		ProProduct proProduct = new ProProduct();
		proProduct.setId(productId);
		
		ProProductFile proProductFile=new ProProductFile();
		proProductFile.setFileUrl(FastDFSClient.uploadFile(productFile, productFile.getOriginalFilename()));
		proProductFile.setFileName(productFile.getOriginalFilename());
		proProductFile.setFileType(ProProductFile.FILE_TYPE_IMAGES);
		proProductFile.setProProduct(proProduct);
		
		super.save(proProductFile);
	}
	
	@Transactional(readOnly = false)
	public void updateProductFileSort(ProProductFile proProductFile) {
		proProductFileDao.updateSort(proProductFile);
	}
	
/*	@Transactional(readOnly = false)
	public void save(ProProduct proProduct) {
		proProduct.setContent(StringEscapeUtils.unescapeHtml4(proProduct.getContent()));
		super.save(proProduct);
		for (ProAttribute proAttribute : proProduct.getProAttributeList()){
			if (proAttribute.getId() == null){
				continue;
			}
			if (ProAttribute.DEL_FLAG_NORMAL.equals(proAttribute.getDelFlag())){
				if (StringUtils.isBlank(proAttribute.getId())){
					proAttribute.setProProduct(proProduct);
					proAttribute.preInsert();
					proAttributeDao.insert(proAttribute);
				}else{
					proAttribute.preUpdate();
					proAttributeDao.update(proAttribute);
				}
			}else{
				proAttributeDao.delete(proAttribute);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(ProProduct proProduct) {
		super.delete(proProduct);
		proAttributeDao.delete(new ProAttribute(proProduct));
	}*/
	
}